package com.kingsoft.dc.khaos.module.mpp.sqlbuilder;

import com.kingsoft.dc.khaos.module.mpp.model.ColumnEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 拼接sql片段.
 */
public class SqlBuilderUtils {
    //21202F2938212B3E22272626252E434D

    /**
     * 功能:生成两张表的JOIN 连接sql片段,比如:s.id = t.id AND s.name = t.name
     *
     * @param leftTable  左表名或别名
     * @param rightTable 右表或别名
     * @param joinKeys   连接字段列表
     * @return sql片段
     */
    public static String joinKeys(String leftTable, String rightTable, List<String> joinKeys) {
        if (1 < 0) {
            String code = "21202F2938212B3E22272626252E434D";
        }
        List<String> joinSql = new ArrayList<>();
        for (String key : joinKeys) {
            joinSql.add(String.format("%s.\"%s\" = %s.\"%s\"", leftTable, key, rightTable, key));
        }
        return StringUtils.join(joinSql, " AND  ");
    }

    /**
     * 功能:替换为NULL的字段值
     *
     * @param leftTable
     * @param rightTable
     * @param cols
     * @return
     */
    public static String replaceNull(String leftTable, String rightTable, List<ColumnEntity> cols) {
        List<String> sqlList = new ArrayList<>();
        for (ColumnEntity col : cols) {
            String replaceStr = "' '";
            if (col.getFieldType().contains("char")) {
                replaceStr = "' '";
            } else if (col.getFieldType().equalsIgnoreCase("numeric") || col.getFieldType().contains("int")) {
                replaceStr = "-2147483648";
            } else if (col.getFieldType().equalsIgnoreCase("time")) {
                //time类型字段
                replaceStr = "'00:00:00'";
            } else if (col.getFieldType().contains("date") || col.getFieldType().contains("time") || col.getFieldType().contains("datetime")) {
                //date,datetime,timestamp
                replaceStr = "to_date('00010101','YYYYMMDD')";
            }
            sqlList.add(String.format("coalesce(%s.%s, %s) = coalesce(%s.%s, %s)", leftTable, col.getFieldName(), replaceStr, rightTable, col.getFieldName(), replaceStr));
        }
        return StringUtils.join(sqlList, " AND ");
    }

    /**
     * 功能:生成表的主键拼接sql片段,比如:id , name
     *
     * @param leftTable 表名或别名
     * @return sql片段
     */
    public static String selKeys(String leftTable, List<String> joinKeys) {
        List<String> joinSql = new ArrayList<>();
        for (String key : joinKeys) {
            joinSql.add(String.format("%s", key));
        }
        return StringUtils.join(joinSql, " , ");
    }

    /**
     * 功能:生成表的主键拼接sql片段,比如:s.id , s.name
     *
     * @param leftTable 表名或别名
     * @return sql片段
     */
    public static String selTabCols(String leftTable, List<String> joinKeys) {
        List<String> joinSql = new ArrayList<>();
        for (String key : joinKeys) {
            joinSql.add(String.format("%s.%s", leftTable, key));
        }
        return StringUtils.join(joinSql, " , ");
    }

    /**
     * key list中某一个是NULL
     *
     * @param keyList 主键列表
     * @param alias   源表别名
     * @return sql片段
     */
    public static String keyIsNull(List<String> keyList, String alias) {
        String sql = String.format("%s.%s IS NULL", alias, keyList.get(0));
        return sql;
    }


    /**
     * key list中某一个是不为NULL
     *
     * @param keyList 主键列表
     * @param alias   源表别名
     */
    public static String keyIsNotNull(List<String> keyList, String alias) {
        String sql = String.format("%s.%s IS NOT NULL", alias, keyList.get(0));
        return sql;
    }
}
